<?php

namespace app\admin\controller;

use app\admin\model\Log;
use app\admin\model\Records;
use app\admin\model\System as SystemModel;
use app\BaseController;
use app\lib\FileDB;
use app\lib\ZFileDB;
use app\admin\model\SupplyCompany;
use app\admin\model\ReceiveCompany;
use app\admin\model\EndUser;
use app\admin\model\ProductName;
use app\Request;

class System extends BaseController
{

    public function getLog(Request $request)
    {
        $cid = input('cid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $ghgs = $request->get('ghgs');
        $shdw = $request->get('shdw');
        $fksj = $request->get('fksj');
        $model1 = FileDB::table('records')->order('id desc');
        $model2 = FileDB::table('records');
        if (!empty($ghgs) || !empty($shdw) || !empty($fksj)) {

            if(!empty($ghgs)){
                $model1->where('ghgs','=',$ghgs);
            }
            if(!empty($shdw)){
                $model1->where('shdw','=',$shdw);
            }
            if(!empty($fksj)){
                $fksj = date('Y-m-d',strtotime($fksj));
                $model1->where('fksj','=',$fksj);
            }
            if (!empty($content4)) {
                $where = [
                    'zdyh', 'like', $content4
                ];
                $model1->where(...$where);
            }
            $list = $model1->selectLimit($perpage * ($page - 1),$perpage);
            $total = $model1->count();
        } else {
            $list = FileDB::table('records')->order('id desc')->selectLimit($perpage * ($page - 1),$perpage);
            $total = FileDB::table('records')->count();
        }

//        $file = @fopen($fileDb->db_file, 'r');
//        // 读取文件数据
//        while ( ($res = fgets($file)) !== false) {
//            $item = trim($res);
//            $item = json_decode($item, true);
//        }

        foreach ($list as &$item) {
            if(!empty($item['ghgs'])){
                $supply = SupplyCompany::detail($item['ghgs']);
                $item['supply_name'] = $supply['company_name']??'';
            }
            if(!empty($item['shdw'])){
                $receive = ReceiveCompany::detail($item['shdw']);
                $item['receive_name'] = $receive['receive_company_name']??'';
            }
            if(!empty($item['zdyh'])){
                $enduser = EndUser::detail($item['zdyh']);
                $item['user_name'] = $enduser['user_name']??'';
            }
            if(!empty($item['value'])){
                $enduser = ProductName::detail($item['value']);
                $item['product_name'] = $enduser['product_name']??'';
            }
            if (!empty($item['cght'])) {
                $arr = [];
                foreach ($item['cght'] as $item2) {
                    $arr[] = $request->domain() . $item2;
                }
                $item['cght_url'] = $arr;
            } else {
                $item['cght_url'] = [];
            }
            if (!empty($item['xsht'])) {
                $arr = [];
                foreach ($item['xsht'] as $item2) {
                    $arr[] = $request->domain() . $item2;
                }
                $item['xsht_url'] = $arr;
            } else {
                $item['xsht_url'] = [];
            }

            if (!empty($item['files'])) {
                $arr = [];
                foreach ($item['files'] as $item2) {
                    $arr[] = $request->domain() . $item2;
                }
                $item['files_url'] = $arr;
            } else {
                $item['files_url'] = [];
            }
        }
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $list
        ];

        return show(config('status.success'), '获取列表', $data);
    }

    public function createLog(Request $request)
    {
        $fksj = $request->post('fksj');
        if(!empty($fksj)){
            $fksj = date('Y-m-d',strtotime($request->post('fksj')));
        }
        $sksj = $request->post('sksj');
        if(!empty($fksj)){
            $sksj = date('Y-m-d',strtotime($request->post('sksj')));
        }

        $data = [
            'ghgs' => $request->post('ghgs'),
            'cght' => $request->post('cght'),
            'cgdj' => $request->post('cgdj'),
            'cgsl' => $request->post('cgsl'),
            'cgjk' => $request->post('cgjk'),
            'fksj' => $fksj,
            'value' =>$request->post('value') ,
            'shdw' => $request->post('shdw'),
            'xsht' => $request->post('xsht'),
            'xsdj' => $request->post('xsdj'),
            'xssl' => $request->post('xssl'),
            'xsjk' => $request->post('xsjk'),
            'sksj' => $sksj,
            'zdyh' => $request->post('zdyh'),
            'files' => ($request->post('files')),
        ];

        FileDB::table('records')->insert($data);
        return show(config('status.success'), '操作成功');
    }

    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $data = SystemModel::order('id asc')->select();
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $name = input('name', '');
        $tel = input('tel', '');
        $map = input('map', '');
        $status = input('status', 0, 'intval');

        $result = SystemModel::create([
            'name' => $name,
            'tel' => $tel,
            'map' => $map,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');

        if ($cid) {
            $data = SystemModel::where('id', $cid)->find();
        } else {
            $data = SystemModel::find($id);
        }

        if ($data) {
            return show(config('status.success'), '查询成功', $data);
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modifySystem()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $name = input('name', '');
        $tel = input('tel', '');
        $desc = input('desc', '');
        $map = input('map', '');
        $status = input('status', 0, 'intval');

        if ($cid) {
            $result = SystemModel::update([
                'name' => $name,
                'tel' => $tel,
                'desc' => $desc
            ], ['id' => $cid]);
        } else {
            $result = SystemModel::update([
                'id' => $id,
                'name' => $name,
                'tel' => $tel,
                'map' => $map,
                'status' => $status
            ]);
        }

        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    /* 修改 */
    public function modifyWechat()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $appid = input('appid', '');
        $appsecret = input('appsecret', '');

        $result = SystemModel::update([
            'appid' => $appid,
            'appsecret' => $appsecret
        ], ['id' => $cid]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }
    //删除
    public function delete($id){
        $result = Records::del($id);
        if ($result) {
            return show(config('status.success'), '操作成功');
        } else {
            return show(config('status.error_update'), '操作失败');
        }
    }
    //编辑
    public function update(Request $request){
        $data = $request->post();
        if(!empty($data['fksj'])){
            $data['fksj'] = date('Y-m-d',strtotime($data['fksj']));
        }
        if(!empty($data['sksj'])){
            $data['sksj'] = date('Y-m-d',strtotime($data['sksj']));
        }
        $result = Records::edit($data);
        if ($result) {
            return show(config('status.success'), '操作成功');
        } else {
            return show(config('status.error_update'), '操作失败');
        }
    }
}
